package beautiful.butterfly.distributed_task_processing_engine.task_manage;

import beautiful.butterfly.distributed_task_processing_engine.jvm_instance_info_and_task_processor_allocation_strategy.JvmInstanceInfoAndTaskProcessorAllocationStrategyUserDefinitionManager;
import beautiful.butterfly.distributed_task_processing_engine.task_manage.task_data_items_processor.task_data_item_data_manager.ITaskAndTaskProcessorAndTaskDataItemManage;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Properties;

/**
 * 任务提交  分配   执行 --引擎
 * DTPE(distributed_task_processing_engine)
 */
@Slf4j
public class DistributedTaskProcessingEngine
{

    public static String configFilePath = null;

    public final static void setConfigFilePath(@NonNull String configFilePath)
    {
        log.warn("configFilePath:" + configFilePath);
        DistributedTaskProcessingEngine.configFilePath = configFilePath;
    }
    //


    public static JvmInstanceStatusAndTaskManage jvmInstanceStatusAndTaskManage;


    public static boolean initial() throws Exception
    {
        if (jvmInstanceStatusAndTaskManage != null)
        {
            return true;
        }
        File file = new File(configFilePath);
        jvmInstanceStatusAndTaskManage = new JvmInstanceStatusAndTaskManage();
        jvmInstanceStatusAndTaskManage.start = false;

        if (file.exists() == true)
        {

            Properties properties = new Properties();
            FileReader reader = new FileReader(file);
            properties.load(reader);
            reader.close();
            //
            log.warn("properties:" + properties);
            jvmInstanceStatusAndTaskManage.init(properties);
            return true;
        } else
        {
            return false;
        }
    }

    public static boolean isInitial() throws Exception
    {
        return jvmInstanceStatusAndTaskManage != null;
    }


    public static ITaskAndTaskProcessorAndTaskDataItemManage getTaskManager() throws Exception
    {
        if (isInitial() == false)
        {
            initial();
        }
        return jvmInstanceStatusAndTaskManage.getTaskAndTaskProcessorAndTaskDataItemManage();
    }

    public static JvmInstanceInfoAndTaskProcessorAllocationStrategyUserDefinitionManager getJvmInstanceInfoAndTaskProcessorUserDefinitionManager() throws Exception
    {
        if (isInitial() == false)
        {
            initial();
        }
        return jvmInstanceStatusAndTaskManage.getJvmInstanceInfoAndTaskProcessorAllocationStrategyUserDefinitionManager();
    }

    public static Properties load() throws IOException
    {
        File file = new File(configFilePath);
        if (!file.exists())
        {
            System.out.println("configFilePath:" + configFilePath);
            throw new FileNotFoundException(configFilePath);
        } else
        {
            Properties properties = new Properties();
            FileReader fileReader = new FileReader(file);
            properties.load(fileReader);
            fileReader.close();
            return properties;
        }

    }

    public static void saveConfigInfo(Properties properties) throws Exception
    {
        try
        {
            FileWriter fileWriter = new FileWriter(configFilePath);
            properties.store(fileWriter, "");
            fileWriter.close();
        } catch (Exception ex)
        {
            throw new Exception(configFilePath, ex);
        }
        if (jvmInstanceStatusAndTaskManage == null)
        {
            initial();
        } else
        {
            jvmInstanceStatusAndTaskManage.reInit(properties);
        }
    }


}
